// pages/myorder/myorder.js
var date = new Date();
var app = getApp()

//月

var M = (date.getMonth()
  + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);

//日

var D = date.getDate()
  < 10 ? '0' + date.getDate() :
  date.getDate();



var day = M + "月" + D + '日'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: false,
    selected1: true,
    line: {

    },
    Alreadypaid: [{
      qi: "青岛北站",
      zhong: "创智谷",
      status: "取消"
    }, {
      qi: "青岛北站",
      zhong: "创智谷",
      status: "哈哈哈哈"
    }]
  },

  /**
   * 生命周期函数--监听页面加载r
   */
  onLoad: function(options) {
    var that = this
    var openid = wx.getStorageSync("openid")
    var orderId = wx.getStorageSync("orderId")
    wx.request({
      url: app.globalData.url +'/order/wxOrderState_two',
      data: {
        state: 1,
        openid: openid
      },
      success: function(res) {
        console.log(res.data)
that.setData({
  complete: res.data,
  getontime: day
})
      }
    })
    wx.login({
      success(res) {
        // that.data.userInfo.code = res.code;
        console.log(res.code);
        that.setData({
          userInfo: {
            code: res.code
          }
        })
      }
    })
  },
  selected: function(e) {
    var index = e.currentTarget.dataset;
var that = this
    this.setData({
      selected1: false,
      selected: true,
      
      // selected2: false
    })
    var openid = wx.getStorageSync("openid")
    console.log(index.state)
    wx.request({
      url: app.globalData.url +'/order/wxOrderState_two',
      data: {
        state: index.state,
        openid: openid
      },
      success: function (res) {
        console.log(res.data)

        wx.setStorageSync("order_end_site", res.data[0].end_site)
        wx.setStorageSync("order_go_site", res.data[0].go_site)
        wx.setStorageSync("order_lineid", res.data[0].lineid)
        wx.setStorageSync("order_orderid", res.data[0].id)
        wx.setStorageSync("userid", res.data[0].weixinuserid)
        that.setData({
          daizhifu: res.data,
          getontime: day
        })
        
      }
    })
  },
  selected1: function(e) {
  var that = this
    this.setData({
      selected: false,
      selected1: true,
      // selected2: false
    })
    var index = e.currentTarget.dataset;
    console.log(index.state)
    var openid = wx.getStorageSync("openid")

    wx.request({
      url: app.globalData.url +'/order/wxOrderState_two',
      data: {
        state: index.state,
        openid: openid
      },
      success: function (res) {
        console.log(res.data)

that.setData({
  complete:res.data,
  getontime: day
})
      }
    })
  },
  // selected2: function() {
  //   this.setData({
  //     selected: false,
  //     selected1: false,
  //     // selected2: true
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  pay:function(){
    var that = this
    that.shop();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  shop() {
    var that = this;

    var end_site = wx.getStorageSync("order_end_site")
    var go_site = wx.getStorageSync("order_go_site")
    var lineid = wx.getStorageSync("order_lineid")
    var orderid = wx.getStorageSync("order_orderid")
    var userid = wx.getStorageSync("userid")
 
    wx.request({
      url: app.globalData.url +'/wxpay/pay', //仅为示例，并非真实的接口地址
      data: {
        userId: userid,
        lineid: lineid,
        payNum: 1,
        gosite: go_site,
        endsite: end_site,
        orderid: orderid
      },
      success(res) {
        console.log(res.data)
        if (res.data.order == -2) {
          wx.showModal({
            title: "消息",
            content: res.data.msg,
            confirmText: "确定",
            confirmColor: 'green',
            showCancel: false
          })
        } else {
          wx.setStorageSync("orderId", res.data.orderId)
          if (res && res.data) {
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonce_str,
              'package': 'prepay_id=' + res.data.prepay_id,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {

                console.log(res)

                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 1400,
                  success: function () {

                    wx.switchTab({
                      url: '../../pages/riding/riding',
                      success: function (res) { },
                      fail: function (res) { },
                      complete: function (res) { },
                    })
                  }
                })

              },
              'fail': function (res) {

                console.log(res);
                wx.showToast({
                  title: '支付失败',
                  icon: 'none',
                  duration: 1500
                })
              }
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    })
  },
  navriding:function(e){
var index = e.currentTarget.dataset;
wx.navigateTo({
  url: '../../pages/riding/riding?orderId=' + index.orderid,
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})
  }
})